package bugtracking

import grails.converters.JSON
import bugtracking.technicalSupportIssues.OpenDate_Duration_TSI

class TechnicalSupportIssuesDMController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)

        def totalDurationAndOpenedDate = {
            def data = new ArrayList();
            Object[] arr = new Object[3];
            arr[0] = "Opened date"
            arr[1] = "Total Duration"
            arr[2] = "Trend Line"
            data.add(arr);
            Calendar calendar = Calendar.getInstance()
            TechnicalSupportIssuesDM.executeQuery("""SELECT YEAR(creation_ts) AS Year, MONTH(creation_ts),DAY(creation_ts), AVG(totalDuration)
                                                    FROM TechnicalSupportIssuesDM
                                                    WHERE YEAR(creation_ts)>2009
                                                    GROUP BY YEAR(creation_ts), MONTH(creation_ts), DAY(creation_ts)
                                                    ORDER BY YEAR(creation_ts) , MONTH(creation_ts), DAY(creation_ts)""").each {
                calendar.set(it[0], it[1] - 1, it[2])//month is 0-11
                arr = new Object[3];
                arr[0] = calendar.getTime();
                arr[1] = it[3];
                arr[2] = 3
                data.add(arr);
            }
            def a = 0;
            def c = 0;
            def sumX = 0;
            def sumY = 0;
            data.eachWithIndex {Object[] obj, int index ->
                if (index > 0) {
                    a += obj[1] * (index + 1)
                    c += index * index;
                    sumX += (index + 1)
                    sumY += obj[1];
                }
            }
            a *= (data.size() - 1)
            c *= (data.size() - 1)
            def b = sumX * sumY;
            def d = sumX * sumX;
            def slope = 0;
            def den = c - d;
            if (den != 0) {
                slope = (a - b) / den
            }
            def bb = (sumY - slope * sumX) / (data.size() - 1);
            data.eachWithIndex { Object[] obj, int index ->
                if(index>0){
                    obj[2]=slope*index+bb;
                }
            }
            return data as JSON
        }

        [technicalSupportIssuesDMInstanceList: TechnicalSupportIssuesDM.list(params), technicalSupportIssuesDMInstanceTotal: TechnicalSupportIssuesDM.count(), i: totalDurationAndOpenedDate.call()]
    }

    private String getUrl(array) {
        String quantity = "";
        String names = "";
        array.each {
            names += it[0] + "|"
            quantity += it[1] + ","
        }

        def url = "https://chart.googleapis.com/chart?cht=p3&chd=t:" + quantity.substring(0, quantity.length() - 1) + "&chs=550x200&chl=" + names.substring(0, names.length() - 1)
    }

    def create = {
        def technicalSupportIssuesDMInstance = new TechnicalSupportIssuesDM()
        technicalSupportIssuesDMInstance.properties = params
        return [technicalSupportIssuesDMInstance: technicalSupportIssuesDMInstance]
    }

    def save = {
        def technicalSupportIssuesDMInstance = new TechnicalSupportIssuesDM(params)
        if (technicalSupportIssuesDMInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'technicalSupportIssuesDM.label', default: 'TechnicalSupportIssuesDM'), technicalSupportIssuesDMInstance.id])}"
            redirect(action: "show", id: technicalSupportIssuesDMInstance.id)
        }
        else {
            render(view: "create", model: [technicalSupportIssuesDMInstance: technicalSupportIssuesDMInstance])
        }
    }

    def show = {
        def technicalSupportIssuesDMInstance = TechnicalSupportIssuesDM.get(params.id)
        if (!technicalSupportIssuesDMInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'technicalSupportIssuesDM.label', default: 'TechnicalSupportIssuesDM'), params.id])}"
            redirect(action: "list")
        }
        else {
            [technicalSupportIssuesDMInstance: technicalSupportIssuesDMInstance]
        }
    }

    def edit = {
        def technicalSupportIssuesDMInstance = TechnicalSupportIssuesDM.get(params.id)
        if (!technicalSupportIssuesDMInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'technicalSupportIssuesDM.label', default: 'TechnicalSupportIssuesDM'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [technicalSupportIssuesDMInstance: technicalSupportIssuesDMInstance]
        }
    }

    def update = {
        def technicalSupportIssuesDMInstance = TechnicalSupportIssuesDM.get(params.id)
        if (technicalSupportIssuesDMInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (technicalSupportIssuesDMInstance.version > version) {

                    technicalSupportIssuesDMInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'technicalSupportIssuesDM.label', default: 'TechnicalSupportIssuesDM')] as Object[], "Another user has updated this TechnicalSupportIssuesDM while you were editing")
                    render(view: "edit", model: [technicalSupportIssuesDMInstance: technicalSupportIssuesDMInstance])
                    return
                }
            }
            technicalSupportIssuesDMInstance.properties = params
            if (!technicalSupportIssuesDMInstance.hasErrors() && technicalSupportIssuesDMInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'technicalSupportIssuesDM.label', default: 'TechnicalSupportIssuesDM'), technicalSupportIssuesDMInstance.id])}"
                redirect(action: "show", id: technicalSupportIssuesDMInstance.id)
            }
            else {
                render(view: "edit", model: [technicalSupportIssuesDMInstance: technicalSupportIssuesDMInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'technicalSupportIssuesDM.label', default: 'TechnicalSupportIssuesDM'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def technicalSupportIssuesDMInstance = TechnicalSupportIssuesDM.get(params.id)
        if (technicalSupportIssuesDMInstance) {
            try {
                technicalSupportIssuesDMInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'technicalSupportIssuesDM.label', default: 'TechnicalSupportIssuesDM'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'technicalSupportIssuesDM.label', default: 'TechnicalSupportIssuesDM'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'technicalSupportIssuesDM.label', default: 'TechnicalSupportIssuesDM'), params.id])}"
            redirect(action: "list")
        }
    }
}
